<?php


namespace app\manage\model;


use think\Db;
use think\Exception;

class Order
{
    /**
     * 返利商城--订单--列表    ---------------品牌，商品分类
     */
    public function rebateOrderList($filter,$page,$page_size){
        $where=[];
        $where['o.is_del']=1;
        $where['o.source_type']=2;
       // $where['g.shop_type']=1;

        //分类有点特殊
        if (!empty($filter['cate_id'])) {
            $cate_id= $filter['cate_id'];
            $where[]=['exp',Db::raw("FIND_IN_SET($cate_id,g.cate_id)")];
        }

        $keyword = empty($filter['keyword']) ? '' : $filter['keyword'];
        if(!empty($keyword)){
            $where['g.title|og.spec_name'] = ['like', "%".$keyword."%"];
        }

        //查询传入时间当天的数据  0-24点
        $endTime = empty($filter['end_time']) ? 0 : strtotime($filter['end_time']);
        $start=strtotime('0:0:0',$endTime);
        $end=strtotime('24:00:00',$endTime);

        if (!empty($endTime)) {
            $where['o.create_time'] = [['>= time', $start], ['<= time', $end]];
        }

        $field='o.order_id,m.avatarUrl,o.receiver,o.re_phone,o.address,og.spec_pic,og.goods_name,
        g.brand_name,og.spec_name,og.goods_num,og.goods_id,g.market_price,g.agreement_price,
        g.cate_id,o.order_coin_money,o.order_score_money,o.order_delivery_fee';

        //dump($where);
        $data=Db::name('order')->alias('o')
            ->field($field)
            ->join('member m','m.member_id=o.member_id')
            ->join('order_goods og','og.order_id=o.order_id')
            ->join('choice_goods g','g.goods_id=og.goods_id')
            ->where($where)
            ->order('o.order_id desc')
            ->page($page,$page_size)
            ->select();
        //dump(Db::name('order')->getLastSql());
        $goods_share=Db::name('system_config')
            ->where(['field_name'=>'rebate_mall_set'])
            ->value('field_value');
        $share=json_decode($goods_share,true);

        if(!empty($data)){
            foreach ($data as $k=>&$v){
                $c_id=substr($v['cate_id'],strripos($v['cate_id'],",")+1);
                //商品分类
                $cate2=Db::name('choice_cate')
                    ->field('name')
                    ->where(['id'=>$c_id])
                    ->find();
                $v['cate_name']=$cate2['name'];

                //市场价
                $v['market_price']=sprintf("%.2f",$v['market_price']/100);
                //协议价
                $v['agreement_price']=sprintf("%.2f",$v['agreement_price']/100);
                //零售价
                $retail_price=getReturnPop($v['goods_id'],1);

                $v['retail_price']=sprintf("%.2f",$retail_price);



                //合计  积分+金币+运费
                $v['total_money']=sprintf("%.2f",$v['order_coin_money']+$v['order_score_money']+$v['order_delivery_fee']);

                //买家积分奖
                $v['buyer']=$share['rebate_buyer_pop'];
                $v['direct']=$share['share_direct_price'];
                $v['inter']=$share['share_inter_price'];
                $v['platform']=$share['platform_price'];

                unset($v['order_coin_money']);
                unset($v['order_score_money']);
                unset($v['order_delivery_fee']);
                unset($v['cate_id']);
                unset($v['goods_id']);
            }
        }

        $total_num=Db::name('order')->alias('o')
            ->join('member m','m.member_id=o.member_id')
            ->join('order_goods og','og.order_id=o.order_id')
            ->join('choice_goods g','g.goods_id=og.goods_id')
            ->where($where)
            ->count();
        $t_page=ceil($total_num / $page_size);

        $list['current_page'] = $page;
        $list['total_page'] =$t_page;
        $list['page_num'] = $page_size;
        $list['total_num'] = $total_num;
        $list['data'] = $data;

        return $list;
    }

    /**
     * 返利商城--分类
     */
    public function cateList(){

        $data=Db::name('choice_cate')
            ->field('id,name')
            ->order('id asc')
            ->select();

        return $data;
    }

    /**
     * 爆品商城--订单--列表
     */
    public function explodeOrderList($filter,$page,$page_size){
        $where=[];
        $where['o.is_del']=1;
        $where['o.source_type']=3;

        //分类有点特殊
        if (!empty($filter['cate_id'])) {
            $cate_id= $filter['cate_id'];
            $where[]=['exp',Db::raw("FIND_IN_SET($cate_id,g.cate_id)")];
        }

        $keyword = empty($filter['keyword']) ? '' : $filter['keyword'];
        if(!empty($keyword)){
            $where['g.title|og.spec_name'] = ['like', "%".$keyword."%"];
        }

        $endTime = empty($filter['end_time']) ? 0 : strtotime($filter['end_time']);
        if (!empty($endTime)) {
            $where['o.create_time'] = ['<= time', $endTime];
        }

        $field='o.order_id,m.avatarUrl,o.receiver,o.re_phone,o.address,og.spec_pic,og.goods_name,
        g.brand_name,og.spec_name,og.goods_num,og.goods_id,g.market_price,g.agreement_price,
        g.cate_id,o.order_coin_money,o.order_score_money,o.order_delivery_fee';

        $data=Db::name('order')->alias('o')
            ->field($field)
            ->join('member m','m.member_id=o.member_id')
            ->join('order_goods og','og.order_id=o.order_id')
            ->join('choice_goods g','g.goods_id=og.goods_id')
            ->where($where)
            ->order('o.order_id desc')
            ->page($page,$page_size)
            ->select();
        //dump(Db::name('order')->getLastSql());

        if(!empty($data)){
            foreach ($data as $k=>&$v){
                $c_id=substr($v['cate_id'],strripos($v['cate_id'],",")+1);
                //商品分类
                $cate2=Db::name('choice_cate')
                    ->field('name')
                    ->where(['id'=>$c_id])
                    ->find();
                $v['cate_name']=$cate2['name'];

                //市场价
                $v['market_price']=sprintf("%.2f",$v['market_price']/100);
                //协议价
                $v['agreement_price']=sprintf("%.2f",$v['agreement_price']/100);
                //零售价
                $retail_price=getReturnPop($v['goods_id'],1);

                $v['retail_price']=sprintf("%.2f",$retail_price);


                //合计  积分+金币+运费
                $v['total_money']=sprintf("%.2f",$v['order_coin_money']+$v['order_score_money']+$v['order_delivery_fee']);



                unset($v['order_score_money']);
                unset($v['order_delivery_fee']);
                unset($v['cate_id']);
                unset($v['goods_id']);
            }
        }

        $total_num=Db::name('order')->alias('o')
            ->join('member m','m.member_id=o.member_id')
            ->join('order_goods og','og.order_id=o.order_id')
            ->join('choice_goods g','g.goods_id=og.goods_id')
            ->where($where)
            ->count();
        $t_page=ceil($total_num / $page_size);

        $list['current_page'] = $page;
        $list['total_page'] =$t_page;
        $list['page_num'] = $page_size;
        $list['total_num'] = $total_num;
        $list['data'] = $data;

        return $list;
    }

    /**
     * 干货分享--订单--列表    1=热门（裂变）  2=精选（返利）--------还要修改---------
     */
    public function shareOrderList($filter,$page,$page_size,$type){
        $where=[];
        $where['o.is_del']=1;
        $where['o.source_type']=4;

        //查询传入时间当天的数据  0-24点
        $endTime = empty($filter['end_time']) ? 0 : strtotime($filter['end_time']);
        $start=strtotime('0:0:0',$endTime);
        $end=strtotime('24:00:00',$endTime);

        if (!empty($endTime)) {
            $where['o.create_time'] = [['>= time', $start], ['<= time', $end]];
        }

        $keyword = empty($filter['keyword']) ? '' : $filter['keyword'];
        if(!empty($keyword)){
            $where['og.goods_name|o.re_phone'] = ['like', "%".$keyword."%"];
        }
        if($type==1){
            $where['g.g_type']=2;
        }else{
            $where['g.g_type']=3;
        }
        //精选视频--积分奖-（直推，间推，平台，买家）
        //热门视频--金币抵扣  order_coin_money
        $field='o.order_id,o.re_phone,m.avatarUrl,m.nickName,og.goods_name,og.spec_pic,
        og.goods_num,o.order_coin_money,o.order_score_money,gs.cost_price,gs.price';

        $data=Db::name('order')->alias('o')
            ->field($field)
            ->join('member m','m.member_id=o.member_id')
            ->join('order_goods og','og.order_id=o.order_id')
            ->join('goods g','g.goods_id=og.goods_id')
            ->join('goods_spec gs','gs.gs_id=og.spec_id')
            ->where($where)
            ->order('o.order_id desc')
            ->page($page,$page_size)
            ->select();

        $goods_share=Db::name('system_config')
            ->where(['field_name'=>'rebate_mall_set'])
            ->value('field_value');
        $config=json_decode($goods_share,true);

        if(!empty($data)){
            foreach ($data as $k=>&$v){
                //合计  积分+金币
                $v['total_money']=sprintf("%.2f",$v['order_coin_money']+$v['order_score_money']);
                if($type==1){
                    //金币抵扣额
                    $v['coin_score']=$v['order_coin_money'];
                }else{
                    //积分奖
                    $v['buyer']=$config['rebate_buyer_pop'];
                    $v['direct']=$config['share_direct_price'];
                    $v['inter']=$config['share_inter_price'];
                    $v['platform']=$config['platform_price'];
                }
                unset($v['order_coin_money']);
                unset($v['order_score_money']);
            }
        }

        $total_num=Db::name('order')->alias('o')
            ->join('member m','m.member_id=o.member_id')
            ->join('order_goods og','og.order_id=o.order_id')
            ->join('goods g','g.goods_id=og.goods_id')
            ->join('goods_spec gs','gs.gs_id=og.spec_id')
            ->where($where)
            ->count();
        $t_page=ceil($total_num / $page_size);

        $list['current_page'] = $page;
        $list['total_page'] =$t_page;
        $list['page_num'] = $page_size;
        $list['total_num'] = $total_num;
        $list['data'] = $data;

        return $list;

    }

    /**
     * 干货分享,返利商城,爆品商城，待收货，待发货---订单--批量删除
     */
    public function shareDelAll($id){
        Db::name('order')
            ->where(['order_id'=>['in',$id]])
            ->update(['is_del'=>2,'update_time'=>time()]);

        return true;
    }

    /**
     * 干货分享,返利商城---订单--详情  type 1=返利 2=爆品  3=干货分享
     */
    public function orderDetail($id,$type){

        $field='status,order_sn,create_time,pay_time';
        $data=Db::name('order')
            ->field($field)
            ->where('order_id',$id)
            ->find();

        $order=Db::name('order')->where('order_id',$id)->find();
        if($order['store_id']>0){
            $data['company_name']=Db::name('store_apply')
                ->where('sa_id',$order['store_id'])->value('company_name');
        }else{
            if($type==1){
                $data['company_name']='民脉返利商城';
            }elseif($type==2){
                $data['company_name']='民脉爆品商城';
            }else{
                $data['company_name']='民脉干货分享';
            }

        }

        return $data;
    }

    /**
     * 评价列表  type  2=待回复  1=已回复  3=全部
     */
    public function commentList($filter,$page,$page_size,$type){
        $where=[];

        //查询传入时间当天的数据  0-24点
        $endTime = empty($filter['end_time']) ? 0 : strtotime($filter['end_time']);
        $start=strtotime('0:0:0',$endTime);
        $end=strtotime('24:00:00',$endTime);

        if (!empty($endTime)) {
            $where['c.create_time'] = [['>= time', $start], ['<= time', $end]];
        }

        $keyword = empty($filter['keyword']) ? '' : $filter['keyword'];
        if(!empty($keyword)){
            $where['og.goods_name|m.nickName'] = ['like', "%".$keyword."%"];
        }

        if($type==1){
            $where['c.is_reply']=1;
        }elseif($type==2){
            $where['c.is_reply']=2;
        }else{
            $where['c.is_reply']=['in',[1,2]];
        }

        $field='c.c_id,m.avatarUrl,m.nickName,og.goods_name,c.star,c.content,c.create_time';

        $data=Db::name('comment')->alias('c')
            ->field($field)
            ->join('member m','m.member_id=c.member_id')
            ->join('order_goods og','og.og_id=c.order_goods_id')
            ->where($where)
            ->order('c.c_id desc')
            ->page($page,$page_size)
            ->select();

        $total_num=Db::name('comment')->alias('c')
            ->join('member m','m.member_id=c.member_id')
            ->join('order_goods og','og.og_id=c.order_goods_id')
            ->where($where)
            ->count();

        $t_page=ceil($total_num / $page_size);

        $list['current_page'] = $page;
        $list['total_page'] =$t_page;
        $list['page_num'] = $page_size;
        $list['total_num'] = $total_num;
        $list['data'] = $data;

        return $list;
    }

    /**
     * 评价回复
     */
    public function commentReply($data){
        $id=$data['c_id'];
        $info['reply']=$data['reply'];
        $info['is_reply']=1;
        $info['update_time']=time();

        Db::name('comment')
            ->where('c_id',$id)
            ->update($info);

        return true;
    }

    /**
     * 评价----删除，批量删除  type  1=单个删除  2=批量删除
     */
    public function commentDel($id,$type){
        if($type==1){
            Db::name('comment')
                ->where('c_id',$id)
                ->delete();
        }else{
            Db::name('comment')
                ->where(['c_id'=>['in',$id]])
                ->delete();
        }

        return true;
    }

    /**
     * 评价---详情
     */
    public function commentDetail($id){

        $data=Db::name('comment')
            ->field('c_id,content,star,reply')
            ->where('c_id',$id)
            ->find();

        return $data;
    }

    /**
     * 待发货,待收货--订单--列表   type  1=待发货  2=待收货  3=已完成
     */
    public function orderList($filter,$page,$page_size,$type){
        $where=[];
        $where['o.is_del']=1;

        //查询传入时间当天的数据  0-24点
        $endTime = empty($filter['end_time']) ? 0 : strtotime($filter['end_time']);
        $start=strtotime('0:0:0',$endTime);
        $end=strtotime('24:00:00',$endTime);

        if (!empty($endTime)) {
            $where['o.create_time'] = [['>= time', $start], ['<= time', $end]];
        }

        $keyword = empty($filter['keyword']) ? '' : $filter['keyword'];
        if(!empty($keyword)){
            $where['og.goods_name|m.nickName'] = ['like', "%".$keyword."%"];
        }

        if($type==1){
            $where['o.status']=2;
            $field='o.order_id,o.order_sn,o.receiver,o.address,m.avatarUrl,m.nickName,og.goods_name,og.spec_pic,
        og.goods_num,og.spec_name,o.order_coin_money,o.order_score_money,o.order_delivery_fee,o.create_time,o.re_phone';

        }elseif($type==2){
            $where['o.status']=3;

            $field='o.order_id,o.order_sn,o.store_id,m.avatarUrl,m.nickName,og.goods_name,og.spec_pic,
        og.goods_num,og.spec_name,o.order_coin_money,o.order_score_money,o.log_sn,o.log_company,o.create_time,
        o.source_type,o.order_delivery_fee,o.re_phone,o.address,o.log_id';

        }else{
            $where['o.status']=['in',[4,5]];

            $field='o.order_id,o.order_sn,o.store_id,m.avatarUrl,m.nickName,og.goods_name,og.spec_pic,
        og.goods_num,og.spec_name,o.order_coin_money,o.order_score_money,o.log_sn,o.log_company,o.create_time,
        o.source_type,o.order_delivery_fee';

        }


        $data=Db::name('order')->alias('o')
            ->field($field)
            ->join('member m','m.member_id=o.member_id')
            ->join('order_goods og','og.order_id=o.order_id')
            ->where($where)
            ->order('o.order_id desc')
            ->page($page,$page_size)
            ->select();


        foreach ($data as $k=>&$v){
            //合计  积分+金币+运费
            $a=$v['order_coin_money']+$v['order_score_money']+$v['order_delivery_fee'];
            $v['total_money']=sprintf("%.2f",$a);

            if($type!=1){
                if($v['store_id']>0){
                    $v['company_name']=Db::name('store_apply')
                        ->where('sa_id',$v['store_id'])->value('company_name');
                }else{
                    if($v['source_type']==2){

                        $v['company_name']='民脉返利商城';

                    }elseif($v['source_type']==3){

                        $v['company_name']='民脉爆品商城';

                    }elseif($v['source_type']==4){

                        $v['company_name']='民脉干货分享';

                    }else{
                        $v['company_name']='民脉联盟商家';
                    }

                }
                unset($v['store_id']);
                unset($v['source_type']);
            }

            unset($v['order_coin_money']);
            unset($v['order_score_money']);
            unset($v['order_delivery_fee']);
        }


        $total_num=Db::name('order')->alias('o')
            ->join('member m','m.member_id=o.member_id')
            ->join('order_goods og','og.order_id=o.order_id')
            ->where($where)
            ->count();
        $t_page=ceil($total_num / $page_size);

        $list['current_page'] = $page;
        $list['total_page'] =$t_page;
        $list['page_num'] = $page_size;
        $list['total_num'] = $total_num;
        $list['data'] = $data;

        return $list;

    }

    /**
     * 商家发货操作页面
     */
    public function deliveryPage($order_id){
        $data=Db::name('order')
            ->field('order_id,order_sn,receiver,re_phone,address,create_time,pay_time')
            ->where(['order_id'=>$order_id])
            ->find();
        return $data;
    }

    /**
     *  发货
     */
    public function deliverGoods($data){
        $info['log_sn']=$data['log_sn'];
        $info['log_company']=$data['log_company'];
        $info['status']=3;
        $info['update_time']=time();

        $order_id=$data['order_id'];

        Db::name('order')
            ->where(['order_id'=>$order_id])
            ->update($data);
        Db::name('order_goods')
            ->where(['order_id'=>$order_id])
            ->update(['og_d_status'=>2,'update_time'=>time()]);

        return true;
    }

    /**
     *  退款
     */
    public function orderRefund($order_id){
        $info=Db::name('order')->alias('o')
            ->field('o.order_id,og.og_id,o.status,o.member_id')
            ->join('order_goods og','og.order_id=o.order_id')
            ->where('o.order_id',$order_id)
            ->find();

        //金币和积分退款    -----------------------微信退款暂时没有写--------------
        $this->refund($info);

        Db::name('order')->where('order_id',$order_id)
            ->update(['status'=>10,'update_time'=>time()]);

        return true;

    }


    /**
     *  提醒收货
     */
    public function receiving($order_id){
        $source_type=Db::name("order")
            ->where(['order_id'=>$order_id])->value("source_type");
        if(!in_array($source_type,[1,2,3])) {
            $order=Db::name('order')
                ->field('order_id,member_id,store_id')
                ->where('order_id',$order_id)
                ->find();

            if($order['store_id']==0){
                $data['member_id']=0;
            }else{
                $store=Db::name('store_apply')
                    ->where('sa_id',$order['store_id'])
                    ->find();
                $data['member_id']=$store['member_id'];
            }

            $data['create_time']=time();
            $data['title']='订单收货提醒';
            $data['content']='您有一条待收货的订单，请尽快查看！';
            $data['object_type']=2;
            $data['object_arr']=$order['member_id'];
            $data['class_type']=13;
            Db::name('message')->insert($data);
        }


        return true;
    }


    /**
     *  查看物流
     */
    public function selectLog($order_id){
        $order=Db::name("order")
            ->where(['order_id'=>$order_id])->find();


    }

    /**
     *  售后订单列表  type  1=全部  2=仅退款  3=退货退款  4=仅退货（暂时不要）
     */
    public function afterOrderList($filter,$page,$page_size,$type){
        $where=[];
        $where['oas.is_del']=1;

        if($type==2){
            $where['oas.after_type']=1;
        }elseif($type==3){
            $where['oas.after_type']=2;
        }else{
            $where['oas.after_type']=['in',[1,2]];
        }

        //查询传入时间当天的数据  0-24点
        $endTime = empty($filter['end_time']) ? 0 : strtotime($filter['end_time']);
        $start=strtotime('0:0:0',$endTime);
        $end=strtotime('24:00:00',$endTime);

        if (!empty($endTime)) {
            $where['oas.create_time'] = [['>= time', $start], ['<= time', $end]];
        }

        $keyword = empty($filter['keyword']) ? '' : $filter['keyword'];
        if(!empty($keyword)){
            $where['og.goods_name|m.nickName'] = ['like', "%".$keyword."%"];
        }

        $field='oas.oas_id,oas.after_sn,m.nickName,o.receiver,og.goods_name,og.spec_pic,
        og.goods_num,og.spec_name,o.order_coin_money,o.order_score_money,o.order_delivery_fee,
        oas.after_type,oas.create_time,oas.store_id,oas.status,oas.type,oas.address,oas.re_phone,o.source_type';

        $data=Db::name('order_after_sale')->alias('oas')
            ->field($field)
            ->join('member m','m.member_id=oas.member_id')
            ->join('order o','o.order_id=oas.order_id')
            ->join('order_goods og','og.order_id=o.order_id')
            ->where($where)
            ->page($page,$page_size)
            ->select();

        if(!empty($data)){
            foreach ($data as $k=>&$v){
                //合计  积分+金币+运费
                $a=$v['order_coin_money']+$v['order_score_money']+$v['order_delivery_fee'];
                $v['total_money']=sprintf("%.2f",$a);
                //店铺名称
                if($v['store_id']>0){
                    $v['company_name']=Db::name('store_apply')
                        ->where('sa_id',$v['store_id'])->value('company_name');
                }else{
                    if($v['source_type']==2){

                        $v['company_name']='民脉返利商城';

                    }elseif($v['source_type']==3){

                        $v['company_name']='民脉爆品商城';

                    }elseif($v['source_type']==4){

                        $v['company_name']='民脉干货分享';

                    }else{
                        $v['company_name']='民脉联盟商家';
                    }

                }

                unset($v['store_id']);
                unset($v['source_type']);
                unset($v['order_coin_money']);
                unset($v['order_score_money']);
                unset($v['order_delivery_fee']);
            }
        }

        $total_num=Db::name('order_after_sale')->alias('oas')
            ->join('member m','m.member_id=oas.member_id')
            ->join('order o','o.order_id=oas.order_id')
            ->join('order_goods og','og.order_id=o.order_id')
            ->where($where)
            ->count();
        $t_page=ceil($total_num / $page_size);

        $list['current_page'] = $page;
        $list['total_page'] =$t_page;
        $list['page_num'] = $page_size;
        $list['total_num'] = $total_num;
        $list['data'] = $data;

        return $list;
    }

    /**
     *  售后----批量删除
     */
    public function afterOrderDel($id){
        Db::name('order_after_sale')
            ->where(['oas_id'=>['in',$id]])
            ->update(['is_del'=>2,'update_time'=>time()]);

        return true;
    }


    /**
     * 售后---同意，拒绝操作
     *          status  1=同意  2=拒绝
     */
    public function afterRefund($data,$status){

        $info=Db::name('order_after_sale')
            ->field('order_id,order_goods_id as og_id,store_id,order_status,member_id')
            ->where('oas_id',$data['oas_id'])->find();

        if($status==1){
            //金币和积分退款    -----------------------微信退款暂时没有写--------------
            $this->refund($info);
            Db::name('order_after_sale')
                ->where(['oas_id'=>$data['oas_id']])
                ->update(['status'=>5]);

            Db::name('order')->where('order_id',$info['order_id'])
                ->update(['status'=>10]);


        }else{
            Db::name('order_after_sale')
                ->where(['oas_id'=>$data['oas_id']])
                ->update(['status'=>3,'refuse_reason'=>$data['refuse_reason']]);

            Db::name('order')
                ->where(['member_id'=>$info['member_id'],'order_id'=>$info['order_id']])
                ->update(['status'=>$info['order_status'],'update_time'=>time()]);
        }

        return true;
    }


    /**
     * 退款----------实际操作 积分，金币------------
     */
    public function refund($input){
        $orderID=$input['order_id'];
        $ogId=$input['og_id'];
        //获取订单商品信息
        $orderGoods=Db::name("order_goods")->where(['og_id'=>$ogId,'order_id'=>$orderID])->find();
        //获取记录
        $assetLogList=Db::name("member_asset_log")
            ->where(['relation_id' => $orderID,'change_type'=>3,'relation_sid'=>$ogId,'log_type_id'=>['in',['58,29,47,60']]])
            ->select();
        $store_id=Db::name("order")->where(['order_id'=>$orderID])->value("store_id");
        Db::startTrans();
        try{
            //7天信誉记录资产处理
            foreach ($assetLogList as $log){
                $member_id=$log['member_id'];
                $log_type_id=$log['log_type_id'];
                $amount=$log['amount'];
                $mal_id=$log['mal_id'];
                $num=Db::name("member_asset_log")
                    ->where(['member_id'=>$member_id,'mal_id'=>$mal_id])
                    ->delete();
                if($num<=0){
                    throw new Exception("更新失败");
                }
                if(in_array($log_type_id,[58,47,29])){//删除推荐赠送 7天信誉冻结
                    $num=Db::name("member_asset")->where(['member_id'=>$member_id])
                        ->setDec("seven_frozen_score", $amount);
                    if($num<=0){
                        throw new Exception("更新失败");
                    }
                }elseif($log_type_id==60){//商家卖出扣除金币退还
                    if($store_id>0){
                        $store_member_id=Db::name("store_apply")->where(['sa_id'=>$store_id])->value("member_id");
                        $sql="update mm_member_asset set coin=coin+$amount,frozen_coin=frozen_score-$amount,
                          seven_frozen_coin=seven_frozen_coin-$amount where member_id=".$store_member_id."  
                          and frozen_coin>=$amount and seven_frozen_coin>=$amount";
                        $num=Db::query($sql);
                        if($num<=0){
                            throw new Exception("更新失败");
                        }
                    }
                }
            }
            //退款
            //暂时全部退到积分金币里
            $score_money=$orderGoods['score_money'];
            $coin_money=$orderGoods['coin_money'];
            $order_member_id=Db::name("order")->where(['order_id'=>$orderID])->value("member_id");
            $sql="update mm_member_asset set coin=coin+$coin_money,score=score+$score_money where member_id=".$order_member_id;
            $num=Db::query($sql);
            if($num<=0){
                throw new Exception("更新失败");
            }
            Db::commit();
        }catch (Exception $e){
            Db::rollback();
        }
    }

}